<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Activity extends MY_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();
		
		//Google Analytics ID
		$this->data['ga_id'] = 18;		
				
		//Load Css
		$this->data['css'][] = "activityStyle.css";	
		
		//Load Library
  		$this->load->library('pagination');			
    }
		
	/*Home*/	 
	public function index($category=NULL)
	{
		if($this->input->post('ajax')) $this->ajax_activity_pager(TRUE);
		
		//Load Javascript
		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";
				
		//Load Module
		$this->data['module'] = $this->module('activity');
		
		//Activity Recent
		$this->data['dataActivityRecent'] = $this->db->select('t.activity_id,j.activity_subject,t.activity_date_end,j.activity_brief')
		->join('activity_info as j','j.activity_id=t.activity_id','INNER')
		->order_by('t.activity_id DESC')
		->get_where('activity as t',"t.is_publish = '1' AND t.activity_date_end > NOW()")
		->result();
		foreach($this->data['dataActivityRecent'] as $value):
			$value->image = $this->db->order_by("activity_image_order ASC,activity_image_id DESC")->get_where('activity_image',array('activity_id' => $value->activity_id),1)->row();
		endforeach;		
		
		//Activity Last
		$this->data['dataActivityLast'] = $this->ajax_activity_pager();		
			
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}
	
	private function ajax_activity_pager($ajax = FALSE)
	{
		$searchCondition = "t.is_publish = '1' AND (t.activity_date_end <= NOW() OR t.activity_date_end IS NULL)";
		$order_by = "t.activity_date_end DESC,t.activity_id DESC";	
		/*Pager*/		
		$config['base_url'] = current_url().'?'.query_string(array('p'));
		$config['total_rows'] = $this->db->from('activity as t')
		->where($searchCondition)
		->count_all_results();
		$config['per_page'] = 10;
		$config['num_links'] = 5;
		$config['first_link'] = '&laquo;';
		$config['first_tag_open'] = '<span class="first">';
		$config['first_tag_close'] = '</span>';				
		$config['last_link'] = '&raquo;';
		$config['last_tag_open'] = '<span class="last">';
		$config['last_tag_close'] = '</span>';		
		$config['next_tag_open'] = '<span class="prev">';
		$config['next_tag_close'] = '</span>';	
		$config['prev_tag_open'] = '<span class="next">';
		$config['prev_tag_close'] = '</span>';			
		$config['cur_tag_open'] = '<span class="num_tag current">';
		$config['cur_tag_close'] = '</span>';
		$config['num_tag_open'] = '<span class="num_tag">';
		$config['num_tag_close'] = '</span>';				
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();
		$this->data['dataActivity'] = $this->db->select('t.activity_id,j.activity_subject,t.activity_date_end,t.activity_auth_date,j.activity_brief')
		->join('activity_info as j','j.activity_id=t.activity_id','INNER')	
		->order_by($order_by)	
		->get_where('activity as t',$searchCondition,$config['per_page'],intval($this->input->get('p')))
		->result();
		
		foreach($this->data['dataActivity'] as $value):
			$value->image = $this->db->order_by("activity_image_order ASC,activity_image_id DESC")->get_where('activity_image',array('activity_id' => $value->activity_id),1)->row();
		endforeach;
		
		if($ajax):
			echo $this->load->view($this->router->class."/ajax_activity_pager", $this->data, TRUE);
			exit;
		else:
			return $this->load->view($this->router->class."/ajax_activity_pager", $this->data, TRUE);		
		endif;			
	}
	
	/*Archives*/
	public function archives($id = NULL)
	{	
		$is_exist = $this->db->from('activity')->where(array('activity_id' => $id,'is_publish' => '1'))->count_all_results();
		if(empty($is_exist)) $this->page_missing();
		
		//Load Module
		$this->data['module'] = $this->module('activity');
		$this->data['module_facebook_plugins_share'] = $this->module_facebook_plugins_share();
		$this->data['module_facebook_plugins_comment'] = $this->module_facebook_plugins_comment();
		
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";		
				
		$this->data['dataResults'] = $this->db->select('t.activity_id,t.activity_date_end,t.activity_auth_date,j.activity_subject,j.activity_brief,j.activity_detail')
		->join('activity_info as j','j.activity_id=t.activity_id','INNER')
		->order_by('t.activity_date_end DESC,t.activity_id DESC')
		->get_where('activity as t',"t.activity_id = '".$id."' AND t.is_publish = '1'",1)
		->row();
		$this->data['dataResults']->image = $this->db->order_by("activity_image_order ASC,activity_image_id DESC")->get_where('activity_image',array('activity_id' => $this->data['dataResults']->activity_id),1)->row();
		
		//Set SEO Tag
		$this->data['seo_title'] = $this->data['dataResults']->activity_subject;
		$this->data['seo_description'] = $this->data['dataResults']->activity_brief;
		$this->data['fb_title'] = $this->data['seo_title'];
		$this->data['fb_description'] = $this->data['seo_description'];
		if(!empty($this->data['dataResults']->image)) $this->data['fb_image'] = image_asset_url('../uploads/activity/images/original/'.$this->data['dataResults']->image->activity_file_name.'.'.$this->data['dataResults']->image->activity_file_ext);
		$this->data['fb_type'] = 'event';
		
		//GA Events
		$this->data['ga_event'] = 'CI-'.$id.'-'.seo_url($this->data['dataResults']->activity_subject);			

		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}
}


/* End of file video.php */
/* Location: ./application/controllers/video.php */